package com.example.itshome.network;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;
import com.example.itshome.activity.LoginPageActivity;
import com.example.itshome.activity.SetPageActivity;
import com.example.itshome.manager.UserInfoManager;
import com.example.itshome.util.SharePreferencesUtil;

public class LogoutRequest extends AbstractNetworkRequest{

    public LogoutRequest setToast(Toast toast) {
        this.toast = toast;
        return this;
    }

    private Toast toast;
    public LogoutRequest(Activity activity) {
        super(activity);
    }

    @Override
    protected String getPathAndParams() {
        return "user/logout?username="+UserInfoManager.getInstance().getUser().getUsername();
    }

    @Override
    protected void handleResult(String result) {
        setHandleResultRunnable(new Runnable() {
            @Override
            public void run() {
                new SharePreferencesUtil(activity,SharePreferencesUtil.USERFile).clearPreferences();
                Intent intent=new Intent(activity, LoginPageActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                activity.startActivity(intent);
            }
        });
    }

    @Override
    protected void handleException(Exception exception) {
        setHandleExceptionRunnable(new Runnable() {
            @Override
            public void run() {
                toast.setText("网络异常");
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.show();
                Intent intent=new Intent(activity, LoginPageActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                activity.startActivity(intent);
            }
        });
    }
}
